<%@page import="guestbook.EntityUser"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.util.List"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%-- <%@ page --%>
<%-- 	import="com.google.appengine.api.datastore.DatastoreServiceFactory"%> --%>
<%-- <%@ page import="com.google.appengine.api.datastore.DatastoreService"%> --%>
<%-- <%@ page import="com.google.appengine.api.datastore.Query"%> --%>
<%-- <%@ page import="com.google.appengine.api.datastore.Entity"%> --%>
<%-- <%@ page import="com.google.appengine.api.datastore.FetchOptions"%> --%>
<%-- <%@ page import="com.google.appengine.api.datastore.Key"%> --%>
<%-- <%@ page import="com.google.appengine.api.datastore.KeyFactory"%> --%>

<%@ page import="javax.persistence.Query" %>

<%@ page import="guestbook.EMFService" %>
<%@ page import="guestbook.EntityUser" %>
<%@ page import="javax.persistence.EntityManager" %>
<%@ page import="javax.persistence.PersistenceContext" %>


<html>
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
</head>
<body>
123

	<%
/* 		String guestbookName = request.getParameter("guestbookName");
		if (guestbookName == null) {
			guestbookName = "default";
		} */
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		if (user != null) {
	%>
	<p>
		Hello,<%=user.getNickname()%>! (You can <a
			href="<%=userService.createLogoutURL(request.getRequestURI())%>">sign
			out</a>.)
	</p>
	<%
		} else {
	%>
	<p>
		Hello! <a
			href="<%=userService.createLoginURL(request.getRequestURI())%>">Sign
			in</a> to include your name with greetings you post.
	</p>
	<%
		}
	%>
	

	<%
/* 		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName); 

		Query query = new Query("Greeting", guestbookKey).addSort("date",Query.SortDirection.DESCENDING);
		List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));*/
		
		List<EntityUser> entityuUserList = EntityUser.getEntityUserList();
		
		if (entityuUserList.isEmpty()) {
	%>
	<p>
		Guestbook has no messages.
	</p>
	<%
		} else {
	%>
	<p>
		Messages in Guestbook.
	</p>
	<%
		for (EntityUser entityUser : entityuUserList) {
				if (entityUser.getUser() == null) {
	%>
	<p>An anonymous person wrote: NO : <%=entityUser.getLowno() %> </p>
	<%
		} else {
	%>
	<p>
		<b>NO : <%=entityUser.getLowno() %> - <%=((User) entityUser.getUser()).getNickname()%></b>
		wrote:
	</p>
	<%
		}
	%>
	<blockquote><%=entityUser.getContent()%></blockquote>
	<%
		}
		}
	%>

	<form action="/sign" method="post">
		<div>
			<textarea name="content" rows="3" cols="60"></textarea>
		</div>
		<div>
			<input type="submit" value="Post UserMessage" />
		</div>
	</form>
</body>
</html>